﻿using CommunityToolkit.Mvvm.Input;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace ChromeTabsTest.ViewModel;

public interface IViewModelPinnedTabExampleWindow
{
    RelayCommand<TabBase> PinTabCommand { get; set; }
}

public class ViewModelPinnedTabExampleWindow : ViewModelExampleBase, IViewModelPinnedTabExampleWindow
{
    public RelayCommand<TabBase> PinTabCommand { get; set; }


    public ViewModelPinnedTabExampleWindow()
    {
        TabBase vm1 = CreateTab1();
        vm1.IsPinned = true;
        ItemCollection.Add(vm1);
        ItemCollection.Add(CreateTab2());
        ItemCollection.Add(CreateTab3());
        ItemCollection.Add(CreateTabLoremIpsum());
        SelectedTab = ItemCollection.FirstOrDefault();
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemCollection);
        //This sort description is what keeps the source collection sorted, based on tab number. 
        //You can also use the sort description to manually sort the tabs, based on your own criterias,
        //as show below by sorting both by tab number and Pinned status.
        view.SortDescriptions.Add(new SortDescription("IsPinned", ListSortDirection.Descending));
        view.SortDescriptions.Add(new SortDescription("TabNumber", ListSortDirection.Ascending));

        PinTabCommand = new RelayCommand<TabBase>(PinTabCommandAction);
    }
    private void PinTabCommandAction(TabBase tab)
    {
        tab.IsPinned = !tab.IsPinned;
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemCollection);
        view.Refresh();
    }

   
}